package com.client.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JLabel;

import java.awt.Component;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Window;

import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Uprawnienia extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String user = null;
					Uprawnienia frame = new Uprawnienia(user);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Uprawnienia(final String user) {
		setTitle("mClient - Uprawnienia");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 447, 358, 195);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		table = new JTable();
		table.setShowVerticalLines(false);
		table.setShowHorizontalLines(false);
		table.setShowGrid(false);
		table.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select index_name from indexy join uprawnienia using(id_index) where id_user =(select id_user from user where username="+quotate(user)+");";          
		        System.out.println(sql);
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            /*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
		            Statement stmt= con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		              
		                String nazwa= rs.getString("index_name");
		                System.out.println(nazwa);
		               // String kurs= rs.getString("Kurs");
		               // String obroty= rs.getString("Obroty");

		                model.addRow(new Object []{
		                    nazwa
		                });			    
		            }
		        }
		        catch(Exception e) {
		           // JOptionPane.showMessageDialog(this, e.getMessage());
		        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"New column"
			}
		));
		table.setBounds(84, 42, 127, 54);
		contentPane.add(table);
		
		JButton btnNewButton = new JButton("Wczytaj ponownie");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.setBounds(84, 104, 127, 23);
		contentPane.add(btnNewButton);
		
		JLabel label1 = new JLabel("New label");
		label1.setBounds(100, 138, 79, 14);
		contentPane.add(label1);
		label1.setText(user);
		
		JLabel lblTwojeUprawnienia = new JLabel("Twoje uprawnienia");
		lblTwojeUprawnienia.setForeground(SystemColor.textHighlight);
		lblTwojeUprawnienia.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblTwojeUprawnienia.setBounds(10, 11, 147, 19);
		contentPane.add(lblTwojeUprawnienia);
		
		JLabel label = new JLabel("mClient");
		label.setForeground(new Color(0, 128, 128));
		label.setFont(new Font("Tahoma", Font.BOLD, 26));
		label.setBounds(221, 5, 110, 23);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\comp.png"));
		label_1.setBounds(10, 42, 64, 74);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("Zalogowany jako: ");
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_2.setBounds(10, 138, 88, 14);
		contentPane.add(label_2);
		
		JButton button = new JButton("Zamknij okno");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Window frame = SwingUtilities.windowForComponent((Component) e
	                    .getSource());
	            frame.setVisible(false);
			}
		});
		button.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button.setBounds(221, 42, 110, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("Zamknij");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button_1.setBounds(221, 76, 110, 23);
		contentPane.add(button_1);
	}
	public String quotate(String content){
	    
        return "'"+content+"'";
    }
}
